// 文章接口
var express = require('express');
var router = express.Router();
const sql = require('../conf/sql.js')
const pool = require('../conf/db.js')
// 获取文章列表
router.get('/', function(req, res, next) {
  let cid = req.query.id ? req.query.id : 1
  console.log(cid);
  pool.query(sql.articles, [cid], (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    }else {
      res.json({
        code: 200,
        msg: 'get articlelist success',
        data: result
      })
    }
    
  })
  
});

//获取文章详情
router.get('/detail', function(req, res, next) {
  
  let id = req.query.id

  console.log(id);
  pool.query(sql.article_detail, [id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    // console.log(result);
    if (result.length == 0) {
      res.json({
        code: 201,
        msg: 'failed',
      })
    }else {
      res.json({
        code: 200,
        msg: 'get articledetail success',
        data: result
      })
    }
    
  })
  
});
module.exports = router;
